package sdufe.lele.controller;


import org.apache.shiro.util.ThreadContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sdufe.lele.annotation.authToken;
import sdufe.lele.dao.MessageMapper;
import sdufe.lele.result.ListResult;
import sdufe.lele.result.MapResult;
import sdufe.lele.result.Result;
import sdufe.lele.uitl.ListUtil;

import javax.annotation.Resource;
import java.util.*;


@RequestMapping("/graph")
@RestController
public class GraphController {

    @Resource
    MessageMapper messageMapper;

    @GetMapping("/line")
    @authToken
    public Result line_graph() {
        MapResult mr = new MapResult();
        String phone = (String) ThreadContext.get("user-phone");
        ArrayList<HashMap<String, Object>> res;
        if (phone.equals("admin")) {
            res = messageMapper.sumByMonth();
        } else {
            res = messageMapper.sumByMonthByPhone(phone);
        }

        ArrayList<String> date_list = new ArrayList<>();
        ArrayList<Double> in_list = new ArrayList<>();
        ArrayList<Double> out_list = new ArrayList<>();

        for (HashMap<String, Object> hm : res) {
            date_list.add((String) hm.get("months"));
            if (hm.get("messageLevel").equals(1)) {
                in_list.add((Double) hm.get("sum"));
            } else {
                out_list.add((Double) hm.get("sum"));
            }

        }

        mr.put("categories", ListUtil.removeStringListDupli(date_list));
        mr.put("income", in_list);
        mr.put("outcome", out_list);

        return mr;
    }






    @GetMapping("/bar")
    @authToken
    public Result bar_graph() {
        MapResult mapResult = new MapResult();
        String phone = (String) ThreadContext.get("user-phone");

        ArrayList<Integer> res;

        ArrayList<String> in_type = new ArrayList<>();
        ArrayList<Double> in_value = new ArrayList<>();
        ArrayList<String> out_type = new ArrayList<>();
        ArrayList<Double> out_value = new ArrayList<>();
        if (phone.equals("admin")) {
            res = messageMapper.selectInAndOutcome();

            for (HashMap<String,Object> i:messageMapper.selectIncomeGroupByType()){
                in_type.add((String) i.get("name"));
                in_value.add((Double) i.get("value"));
            }
            for (HashMap<String,Object> i:messageMapper.selectOutcomeGroupByType()){
                out_type.add((String) i.get("name"));
                out_value.add((Double) i.get("value"));
            }

        } else {
            res = messageMapper.selectInAndOutcomeByPhone(phone);
            for (HashMap<String,Object> i:messageMapper.selectIncomeGroupByTypeAndPhone(phone,"MONTH")){
                in_type.add((String) i.get("name"));
                in_value.add((Double) i.get("value"));
            }
            for (HashMap<String,Object> i:messageMapper.selectOutcomeGroupByTypeAndPhone(phone,"MONTH")){
                out_type.add((String) i.get("name"));
                out_value.add((Double) i.get("value"));
            }
        }

        mapResult.put("total",res);
        mapResult.put("income_type",in_type);
        mapResult.put("income_value",in_value);
        mapResult.put("outcome_type",out_type);
        mapResult.put("outcome_value",out_value);



        return mapResult;

    }


    @GetMapping("/funnel")
    @authToken
    public Result funnel(@RequestParam(name = "type") String type) {
        MapResult mapResult = new MapResult();
        String phone = (String) ThreadContext.get("user-phone");
        if (type.equals("type")){
            mapResult.put("year_in", messageMapper.selectIncomeGroupByTypeAndPhone(phone,"YEAR"));
            mapResult.put("year_out", messageMapper.selectOutcomeGroupByTypeAndPhone(phone,"YEAR"));
            mapResult.put("month_in",  messageMapper.selectIncomeGroupByTypeAndPhone(phone,"MONTH"));
            mapResult.put("month_out",  messageMapper.selectOutcomeGroupByTypeAndPhone(phone,"MONTH"));
        }else if (type.equals("broker")){
            mapResult.put("year_in", messageMapper.selectInOutGroupByBroker(phone,"YEAR",1));
            mapResult.put("year_out", messageMapper.selectInOutGroupByBroker(phone,"YEAR",2));
            mapResult.put("month_in",  messageMapper.selectInOutGroupByBroker(phone,"MONTH",1));
            mapResult.put("month_out",  messageMapper.selectInOutGroupByBroker(phone,"MONTH",2));
        }


        return mapResult;

    }
}
